﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace xboxssbeatemup
{
    class Sprite : Microsoft.Xna.Framework.Game
    {
        public float X = 0.0f;
        public float Y = 0.0f;
        public int Width = 0;
        public int Height = 0;
        //public Vector2 position = Vector2.Zero;
        public float speed = 1.0f;
        public Vector2 velocity = Vector2.Zero;
        public Vector2 acceleration = Vector2.Zero;

        private ContentManager _content;
        // Spinning rings animation variables
        private String _texturePath;
        private Texture2D _texture;
        private Point frameSize;
        private Point currentFrame = new Point(0, 0);
        private Point sheetSize;
        private int spritesPerLastRow;
        private int timeSinceLastFrame = 0;
        private int timePerFrame; // in ms
        private Boolean isAnimating = true;
        private Boolean isMoving = true;

        // Constructor
        public Sprite(ContentManager content, String path, Point spriteDimensions, Point sheetDimensions, int lastRow, int _timePerFrame)
        {
            _content = content;
            _texturePath = path;
            frameSize = spriteDimensions;
            sheetSize = sheetDimensions;
            spritesPerLastRow = lastRow;
            timePerFrame = _timePerFrame;
            Width = frameSize.X;
            Height = frameSize.Y;
        }

        public void Play()
        {
            isAnimating = true;
        }

        public void Stop()
        {
            isAnimating = false;
        }

        new public void LoadContent()
        {
            _texture = _content.Load<Texture2D>(_texturePath);
        }

        new public void Update(GameTime gameTime)
        {
            if (isAnimating)
            {
                // Animating rings update code
                timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
                if (timeSinceLastFrame > timePerFrame)
                {
                    timeSinceLastFrame -= timePerFrame;
                    ++currentFrame.X;
                    if (currentFrame.X >= sheetSize.X
                        || (currentFrame.X >= spritesPerLastRow && currentFrame.Y >= sheetSize.Y - 1))
                    {
                        currentFrame.X = 0;
                        ++currentFrame.Y;
                        if (currentFrame.Y >= sheetSize.Y)
                            currentFrame.Y = 0;
                    }
                }
            }
            if (isMoving)
            {
                X += velocity.X;
                Y += velocity.Y;
                velocity.X += acceleration.X;
                velocity.Y += acceleration.Y;
            }
        }

        public void Draw(SpriteBatch sb)
        {
            sb.Draw(_texture, new Vector2(X,Y),
            new Rectangle(currentFrame.X * frameSize.X,
                currentFrame.Y * frameSize.Y,
                frameSize.X,
                frameSize.Y),
            Color.White, 0, Vector2.Zero,
            1, SpriteEffects.None, 0);
        }
    }
}
